package com.lhl.bio;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.Scanner;

/**
 * 阻塞IO客户端
 * @author Liu Hanlin
 * @create 2024-10-25 0:59
 */
public class BIOClient {
    public static void main(String[] args) throws IOException {

        OutputStream outputStream = null;
        try (Socket socket = new Socket("127.0.0.1", 8888)) {

            Scanner scanner = new Scanner(System.in);

            System.out.println("输入发送消息（exit退出）:");
            while (scanner.hasNext()){

                String msg = scanner.next();

                if("exit".equals(msg)){
                    scanner.close();
                    break;
                }
                outputStream = socket.getOutputStream();
                outputStream.write(msg.getBytes());
                System.out.printf("发送消息：%s\n", msg);
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally {
            if (outputStream != null){
                outputStream.close();
            }
        }

    }
}
